package com.slwu.configure;

import com.slwu.enums.CommonExceptionEnum;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import javax.annotation.Resource;

@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

    @Resource
    private OpenApiExtensionResolver openApiExtensionResolver;

    /**
     * API文档分组配置
     **/
    @Bean(value = "apiDoc")
    public Docket apiDoc() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("demo")
                        .description("demo")
                        .termsOfServiceUrl("https://www.baidu.com")
                        .contact(new Contact("SLWU","https://www.baidu.com", "bgzydyhhd21@163.com"))
                        .version("1.0.0")
                        .build())
                .globalResponseMessage(RequestMethod.GET, CommonExceptionEnum.responseList())
                .globalResponseMessage(RequestMethod.POST, CommonExceptionEnum.responseList())
                .groupName("demo")
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("com.slwu"))
                .paths(PathSelectors.any())
                .build().extensions(openApiExtensionResolver.buildExtensions("demo"));
    }

}
